package com.ebt.dl.resource;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ebt.dl.domain.Credential;
import com.ebt.dl.service.TokenService;

@Path("/tokens")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
@Component
public class TokenResource {

	@Autowired
	private TokenService tokenService;

	@POST
	public Response authenticateUser(@Valid Credential credential) {

		String userId = credential.getUserId();
		String password = credential.getPassword();

		try {
			String token = tokenService.authenticateUser(userId, password);
			Map<String,String> result=new HashMap<String, String>();
			result.put("token", token);
			return Response.status(Response.Status.CREATED).entity(result).build();

		} catch (Exception e) {
			return Response.status(Response.Status.UNAUTHORIZED).build();
		}

	}
	
//	@DELETE
//	public Response logout(){
//		
//	}

}
